<?php

/**
 * ACoreBaseContainer
 *
 * @author Usenko Timur <usenko.timur@gmail.com>
 * @package base
 */

/**
 * @package base
 */
abstract class ACoreBaseContainer
{
	#<editor-fold defaultstate="collapsed" desc="protected">
	/**
	 * @var array
	 */
	protected $aContainer;
	#</editor-fold>

	/**
	 * @return ACoreBaseContainer
	 */
	public function __construct()
	{
		$this->aContainer = array();
	}

	/**
	 * @param string $sKey
	 * @param mixed $mValue
	 * @return void
	 */
	public function __set($sKey, $mValue)
	{
		$this->SetValue($sKey, $mValue);
	}

	/**
	 * @param string	$sKey
	 * @return mixed
	 */
	public function __get($sKey)
	{
		return $this->GetValue($sKey);
	}

	/**
	 * @param string $sKey
	 * @param string $nsType = null
	 * @return mixed
	 */
	public function GetValue($sKey, $nsType = null)
	{
		CCore::CheckTypes(array('$sKey', '$nsType'));
		
		$bReturn = null;
		if (array_key_exists($sKey, $this->aContainer))
		{
			$bReturn = $this->aContainer[$sKey];
			if (null !== $nsType)
			{
				settype($bReturn, $nsType);
			}
			
			return $bReturn;
		}

		throw new CCoreBaseException('object don\'t contain "'.$sKey.'" key.');
	}

	/**
	 * @param string $sKey
	 * @param mixed $mValue
	 * @param string $sType = null
	 * @return void
	 */
	public function SetValue($sKey, $mValue, $nsType = null)
	{
		CCore::CheckTypes(array('$sKey', '$mValue', '$nsType'));
				
		if (array_key_exists($sKey, $this->aContainer))
		{
			if (null !== $nsType)
			{
				settype($mValue, $nsType);
			}
			$this->aContainer[$sKey] = $mValue;
		}

		throw new CCoreBaseException('object don\'t contain "'.$sKey.'" key.');
	}

	/**
	 * @param array $aValues
	 * @return void
	 */
	public function MassSetValues($aValues)
	{
		CCore::CheckTypes(array('$aValues'));
		
		foreach ($aValues as $sKey => $mValue)
		{
			$this->SetValue($sKey, $mValue);
		}
	}
}